﻿using OpenQA.Selenium;
using SeleniumExtension.Models;
using SeleniumExtension.WebDriver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeleniumExtension.Search
{
    public class LinkText
    {
        private List<Results> results { get; set; }

        public LinkText()
        {
            results = new List<Results>();
        }

        /// <summary>
        /// Search based on link text
        /// </summary>
        /// <param name="searchOptions"></param>
        /// <returns></returns>
        public List<Results> Search(Options searchOptions)
        {
            var dbTestCase = new SeleniumExtension.Services.MockSqlServices().GetSimpleDataObjects();
            foreach (var item in dbTestCase)
            {
                InternetExplorer.Instance.Navigate().GoToUrl(item.Url);
                foreach (var keyword in item.Keywords)
                {
                    try
                    {
                        IWebElement search = InternetExplorer.Instance.FindElement(By.LinkText(keyword));
                        results.Add(new Results()
                        {
                            Keyword = keyword,
                            Status = "Pass",
                            Url = item.Url
                        });
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains(String.Format("Unable to find element with link text == {0}", keyword)))
                        {
                            results.Add(new Results()
                            {
                                Keyword = keyword,
                                Status = "Fail",
                                Url = item.Url
                            });
                        }
                    }

                    if (searchOptions.TakeScreenShot)
                    {
                        Features.TakeScreenshot.Snap(InternetExplorer.Instance, Utilities.Utilities.GetFileName(item.Url));
                    }
                }
            }
            return results;
        }
    }
}
